package com.demo.io;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author : YU.TAN
 * @date : 2021/11/15 14:18
 */
public class TcpClientDemo1 {
    public static void main(String[] args) throws Exception {
        Socket socket = null;
        OutputStream outputStream = null;

        try {
            InetAddress localHost = InetAddress.getLocalHost();
//            InetAddress localHost = InetAddress.getByName("localhost");
            int port = 9000;
            //创建连接
            socket = new Socket(localHost, port);
            //发送消息 IO流
            outputStream = socket.getOutputStream();

            outputStream.write("Hello World! 你好，世界！qwrwqgwagasgdhfdhrfghffffffffffffffffffffffffffffffffffffffffffffffffffffff".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
            if (socket != null) {
                socket.close();
            }
        }
    }
}
